package cn.gyouzhe.source;

import cn.gyouzhe.utils.Common;
import cn.gyouzhe.source.vo.GoodVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 大淘客来源
 *
 * @author wangchuan
 * @datetime 2018/2/12 23:19
 */
class DataokeSource extends Source {
    private String url = "http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=001viudldm&v=2";

    @Override
    String buildUrl(int page) {
        return url + "&page=" + page;
    }

    @Override
    int totalPage(String content) {
        JSONObject jsonObject = JSON.parseObject(content);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        int totalNum = dataObject.getIntValue("total_num");
        return totalNum % 50 == 0 ? totalNum / 50 : totalNum / 50 + 1;
    }

    @Override
    List<GoodVo> format(String content) {
        JSONObject jsonObject = JSON.parseObject(content);
        List<GoodVo> goodVos = new ArrayList<>();
        if(jsonObject.containsKey("result")) {
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            int size = jsonArray.size();
            for(int index = 0; index < size; index++) {
                JSONObject goodJsonObject = jsonArray.getJSONObject(index);
                goodVos.add(formatItem(goodJsonObject));
            }
        }
        return goodVos;
    }

    @Override
    GoodVo formatItem(JSONObject jsonObject) {
        String goodId = jsonObject.getString("GoodsID");
        String title = jsonObject.getString("Title");
        String shortTitle = jsonObject.getString("D_title");
        String des = jsonObject.getString("Introduce");
        int cid = jsonObject.getIntValue("Cid");
        String pic = jsonObject.getString("Pic");
        BigDecimal price = jsonObject.getBigDecimal("Org_Price");
        int tmall = jsonObject.getIntValue("IsTmall");
        BigDecimal commissionJihua = jsonObject.getBigDecimal("Commission_jihua");
        BigDecimal commissionQueqiao = jsonObject.getBigDecimal("Commission_queqiao");
        String sellerId = jsonObject.getString("SellerID");
        int sellNum = jsonObject.getIntValue("Sales_num");
        BigDecimal commission;
        if(commissionJihua.compareTo(commissionQueqiao) >= 1) {
            commission = commissionJihua;
        } else {
            commission = commissionQueqiao;
        }
        String couponId = jsonObject.getString("Quan_id");
        BigDecimal couponPrice = jsonObject.getBigDecimal("Quan_price");
        BigDecimal couponCondition = jsonObject.getBigDecimal("Quan_condition");
        String couponTime = jsonObject.getString("Quan_time");
        return GoodVo.of(
                goodId,
                title,
                shortTitle,
                des,
                pic,
                price,
                commission,
                cid,
                tmall,
                sellNum,
                couponId,
                couponPrice,
                couponCondition,
                buildCouponUrl(goodId, couponId),
                null,
                Common.stringToDatetime(couponTime), sellerId, null);
    }
}
